/**
 * 
 */
package com.planner.core.google.util;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.india.StopNode;
import com.india.metro.delhi.DelhiMetroNode;
import com.planner.core.google.map.Color;
import com.planner.core.google.map.MapType;
import com.planner.core.google.map.MarkerStyle;
import com.planner.core.google.map.Parameter;
import com.planner.core.google.map.PathStyle;
import com.planner.core.google.map.Sensor;
import com.planner.core.google.map.Size;
import com.planner.core.google.map.ValueEnum;
import com.planner.core.google.map.Weight;

/**
 * @author pradeep
 *
 */
public class MapUtil {

	private static final String GOOGLE_BASE_URL = "http://maps.googleapis.com/maps/api/staticmap?";
	public static String constructMap(List<StopNode> path) {
		StringBuffer buffer = new StringBuffer(GOOGLE_BASE_URL);
		//appendParameter(buffer, Parameter.CENTER, path.get(0).getLocation());
		appendParameter(buffer, Parameter.MAP_TYPE, MapType.DEFAULT.getValue());
		//appendParameter(buffer, Parameter.ZOOM, ZoomLevel.DEFAULT.getValue());
		appendParameter(buffer, Parameter.SIZE, Size.DEFAULT);
		appendParameter(buffer, Parameter.SENSOR, Sensor.DEFAULT.getValue());
		appendParameter(buffer, Parameter.PATH, getPathValue(path));
		
		appendParameter(buffer, Parameter.MARKERS, getMarkerValue(path.get(0), Color.BLUE, "S"));
		appendParameter(buffer, Parameter.MARKERS, getMarkerValue(path.get(path.size()-1), Color.YELLOW, "E"));
		List<StopNode> startAndEnd = new ArrayList<StopNode>();
		startAndEnd.add(path.get(0));
		startAndEnd.add(path.get(path.size()-1));
		appendParameter(buffer, Parameter.VISIBLE, getPathAsString(startAndEnd));
		return buffer.toString();
	}
	private static String getMarkerValue(StopNode node, Color color, String label){
		StringBuffer buffer = new StringBuffer();
		appendParameter(buffer, MarkerStyle.COLOR, color.getValue());
		appendParameter(buffer, MarkerStyle.PIPE,"");
		appendParameter(buffer, MarkerStyle.LABEL,label);
		appendParameter(buffer, MarkerStyle.PIPE,"");
		List<StopNode> nodes = new ArrayList<StopNode>();
		nodes.add(node);
		buffer.append(getPathAsString(nodes));
		return buffer.toString();
	}
	
	private static String getPathAsString(List<StopNode> path){
		StringBuffer buffer = new StringBuffer();
		for(int i= 0;i<path.size();i++){
			StopNode node = path.get(i);
			if(i ==0){
				appendParameter(buffer,PathStyle.START , node.getLocation());
			}else{
				appendParameter(buffer,PathStyle.PIPE , node.getLocation());
			}
		}
		return buffer.toString();
	}
	private static String getPathValue(List<StopNode> path){
		StringBuffer buffer = new StringBuffer();
		appendParameter(buffer, PathStyle.COLOR, Color.BLUE.getValue());
		appendParameter(buffer, PathStyle.PIPE,"");
		appendParameter(buffer, PathStyle.WEIGHT, Weight.DEFAULT.getValue());
		for(StopNode node : path){
			appendParameter(buffer,PathStyle.PIPE , node.getLocation());
		}
		return buffer.toString();
	}
	private static void appendParameter(StringBuffer buffer, ValueEnum parameter, Object value) {
		buffer.append(parameter.getValue());
		buffer.append(value);
	}

	public static void main(String[] args) {
		List<StopNode> path = new LinkedList<StopNode>();
		path.add((StopNode)DelhiMetroNode.VAISHALI);
		path.add((StopNode)DelhiMetroNode.KAUSHAMBI);
		System.out.println(constructMap(path));
	}
}
